home *** CD-ROM | disk | FTP | other *** search
/ NeXT Enterprise Objects Framework 1.1 / NeXT Enterprise Objects Framework 1.1.iso / NextDeveloper / Examples / EnterpriseObjects / Validation / Employee.m < prev    next >
Encoding:
Text File  |  1995-02-17  |  2.0 KB  |  82 lines

  1. // Employee.m
  2. //
  3. // Created on Thu Sep 15 17:31:19 PDT 1994 by NeXT EOModeler.app Version 59
  4.  
  5. #import "Employee.h"
  6. #import "Department.h"
  7. #import "ValidatingDelegate.h"
  8.  
  9. @implementation Employee
  10.  
  11. // Accessors
  12. - (double)salary {
  13.      return salary;
  14. }
  15.  
  16.  
  17. // Key Validation 
  18.  
  19. - (NSArray *)keysToValidate
  20. {
  21.     return [NSArray arrayWithObjects:@"salary", @"city", @"address", nil];
  22. }
  23.  
  24. - (NSString *)validateSalary:(NSNumber *)proposed
  25. {
  26.     if ([proposed doubleValue] < 1000.0)
  27.         return @"Salaries must be greater than $1,000";
  28.  
  29.     return  nil; // no error
  30. }
  31.  
  32. - (NSString *)validateCity:(NSString *)proposed
  33. {
  34.     if ([proposed length] == 0)
  35.         return @"You must enter a city";
  36.     return nil;
  37. }
  38.  
  39. - (NSString *)validateAddress:(NSString *)proposed
  40. {
  41.     if ([proposed length] > 40)
  42.         return @"Addresses may not be longer than 40 characters";
  43.     return nil;
  44. }
  45.  
  46. // Final validation ////
  47. - (NSDictionary *)validForDataSource:(id <EODataSources>)dataSource
  48. {
  49.     // By default this message is called for both insertion and update.
  50.     // The superclass implementation will call our per-key validators (above).
  51.     // We can also check any broader validation constraints.  E.g. is salary+bonus
  52.     // at an exceptable level?  In this case we message a related object (department)
  53.     // to see if it's okay with our settings.  Department, in turn, check if our
  54.     // salary is within the department limits.
  55.     NSString *errorMessage;
  56.     const NSString *generalKey = @"General";
  57.     NSDictionary *errors;
  58.  
  59.     if (errors = [super validForDataSource:dataSource])
  60.         return errors;
  61.  
  62.     // check cross property validation constraints
  63.     // Ask our department if it's okay with our new values
  64.     if (errorMessage = [toDepartment validateEmployee:self])
  65.         return [NSDictionary dictionaryWithObjects:&errorMessage forKeys:&generalKey count:1];
  66.  
  67.     return nil;
  68. }
  69.  
  70. - (void)dealloc
  71. {
  72.     [address autorelease];
  73.     [city autorelease];
  74.     [firstName autorelease];
  75.     [lastName autorelease];
  76.     [toDepartment dealloc];
  77.     [super dealloc];
  78. }
  79.  
  80.  
  81. @end
  82.